package com.example.littleken.ormlite_text.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.littleken.ormlite_text.R;
import com.example.littleken.ormlite_text.adapter.PageAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页Activity
 */
public class GuideActivity extends AppCompatActivity {
    private Button btn_openlifediary;
    private ViewPager viewpage;
    //用于保存所有引导页布局
    private List<View> guideViews = new ArrayList<>();
    private PagerAdapter adapter;
    //用于显示下面的点提示
    private List<ImageView> dotViews = new ArrayList<>();
    private ViewGroup dotViewGroup;
    //用于确定是否到达引导页最后一页
    private int num=0;
    //用于保存引导页布局
    private int[] guideLayout = {
            R.layout.guidelayout_first,
            R.layout.guidelayout_second,
            R.layout.guidelayout_thrid,
            R.layout.guidelayout_fourth};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_text);
        num = guideLayout.length-1;
        dotViewGroup = (ViewGroup) findViewById(R.id.dot_layout);
        initLayout();//初始化引导页布局
        initView();
    }
    //初始化所有layout
    private void initLayout() {
        for (int i = 0; i <guideLayout.length ; i++) {
            guideViews.add(getLayoutInflater().inflate(guideLayout[i],null));
            //初始化点提示
            ImageView dot = new ImageView(GuideActivity.this);
            dot.setImageResource(R.drawable.gray);
            dot.setMaxHeight(100);
            dot.setMaxWidth(100);
            //设置点提示具体布局
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20,20);
            layoutParams.leftMargin = 20;
            dot.setLayoutParams(layoutParams);
            dot.setEnabled(false);
            dotViewGroup.addView(dot);
            dotViews.add(dot);

        }
    }

    private void initView() {
        //按钮跳转到MainActivity
        btn_openlifediary = (Button)findViewById(R.id.btn_openlifediary);
        btn_openlifediary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                finish();
            }
        });
        //初始化viewpage
        viewpage = (ViewPager) findViewById(R.id.viewpage);
        adapter = new PageAdapter(guideViews);
        viewpage.setAdapter(adapter);
        viewpage.setOffscreenPageLimit(4);
        viewpage.setCurrentItem(0);
        //修复刚启动，第一个点不显示
        setDotViews(0);//用于实现具体页面与对应点绑定
        viewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setDotViews(position);
                //当引导页到达最后一页，显示跳转按钮
                if(position==num){
                    btn_openlifediary.setVisibility(View.VISIBLE);
                }else{
                    btn_openlifediary.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    /**
     * 用于实现具体页面与对应点绑定
     */
    private void setDotViews(int position) {
        for (int i = 0; i < dotViews.size(); i++) {
            dotViews.get(i).setImageResource(position == i? R.drawable.black:R.drawable.gray );
        }
    }
}
